﻿using Aop.Api.Encryption;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.DAL;
using System.Dto;
using System.IBLL;
using System.IDAL;
using System.IO;
using System.Linq;
using System.Models.Models;
using System.Text;
using System.Threading.Tasks;

namespace System.BLL
{
    public class BatchInputByExcelManager : IBatchInputByExcelManager
    {
        public IEnumerable<int> GetCountToWelcome()
        {
            using (IStoreyService storeySvc = new StoreyService())
            {
                var list = storeySvc.GetAllAsync().ToList().Count();
                yield return list;
            }
            using(IMaterialService MaterialSvc = new MaterialService())
            {
                var list = MaterialSvc.GetAllAsync().ToList().Count();
                yield return list;
            }

        }

        public int[] GetEChartsToWelcome()
        {
            int[] data = new int[6];
            int list;
            using(IStaffService staffSvc = new StaffService())
            {
                list = staffSvc.GetAllAsync().ToList().Count;
                data[0] = list;
            }
            using (IHouseholdService HouseholdSvc = new HouseholdService())
            {
                list = HouseholdSvc.GetAllAsync().ToList().Count;
                data[1] = list;
            }
            using (IPublicityService PublicitySvc = new PublicityService())
            {
                list = PublicitySvc.GetAllAsync().Where(m=>m.ReleaseStatus).ToList().Count;
                data[2] = list;
            }
            using (IUserGuaranteeService UserGuaranteeSvc = new UserGuaranteeService())
            {
                list = UserGuaranteeSvc.GetAllAsync().ToList().Count;
                data[3] = list;
            }
            using (IOnlinSurveyService OnlinSurveySvc = new OnlinSurveyService())
            {
                list = OnlinSurveySvc.GetAllAsync().ToList().Count;
                data[4] = list;
            }
            using (IExpenseInformationService ExpenseInformationSvc = new ExpenseInformationService())
            {
                list = ExpenseInformationSvc.GetAllAsync().ToList().Count;
                data[5] = list;
            }
            return data;
        }

        public List<GetMaterialListDto> getMaterialListDtos()
        {
            using (IMaterialService materialSvc = new MaterialService())
            {
                string week = GetWeek();
                var data = materialSvc.GetAllAsync().Where(m=>m.MaintenanceCycle==week).Select(m=>new GetMaterialListDto() 
                {
                    Id = m.Id,
                    MaterialName = m.MaterialName,
                    MaterialLocation = m.MaterialLocation,
                    MaintenanceCycle = m.MaintenanceCycle,
                    CreateTime = ((DateTime)m.CreateTime).ToString(),
                    Remark = m.Remark
                }).ToList();
                foreach(var ls in data)
                {
                    ls.CreateTime = DateTime.Parse(ls.CreateTime).ToString("yyyy-MM-dd HH:mm:ss");
                }
                return data;
            }
        }
        private string GetWeek()
        {
            string week = string.Empty;
            switch ((int)DateTime.Now.DayOfWeek)
            {
                case 0:
                    week = "周日";
                    break;
                case 1:
                    week = "周一";
                    break;
                case 2:
                    week = "周二";
                    break;
                case 3:
                    week = "周三";
                    break;
                case 4:
                    week = "周四";
                    break;
                case 5:
                    week = "周五";
                    break;
                default:
                    week = "周六";
                    break;
            }
            return week;
        }

        public async Task UserBatchInput(Stream stream, string fileType)
        {
            IWorkbook wk = null;
            CryptoHelper helper = new CryptoHelper();
            List<Household> householdLists = new List<Household>();
            Household household;
            using (IHouseholdService householdSvc = new HouseholdService())
            {
                try
                {

                    if (fileType == ".xls")
                    {
                        wk = new HSSFWorkbook(stream);
                    }
                    else
                    {
                        wk = new XSSFWorkbook(stream);
                    }
                    ISheet sheet = wk.GetSheetAt(0);
                    IRow row;
                    ICell cell;
                    for (int i = 0; i < 100; i++)
                    {
                        household = new Household();
                        row = sheet.GetRow(i + 1);
                        
                        if (row == null)
                            break;
                        household.UserName = row.GetCell(0).ToString();
                        household.Email = row.GetCell(1).ToString();
                        household.Iphone = row.GetCell(2).ToString();
                        household.Sex = row.GetCell(3).ToString() == "男" ? true : false;
                        household.PassWord = helper.Encrypt("123456");
                        household.DepartmentRoleId = 2;
                        await householdSvc.CreateAsync(household,false);
                    }
                    await householdSvc.Save();



                }
                catch (Exception ex)
                {
                    //await householdSvc.Save();
                }
            }
            
        }
    }
}
